#include<iostream>

using namespace std;

//static_cast适用于相近类型的转换
void test1()
{
	int i = 10;
	double d = static_cast<double> (i);

	cout << typeid(i).name() << endl;
	cout << typeid(d).name() << endl;

}

//reinterpret_cast适用于无关类型的转换
void test2()
{
	int n = 10;
	int* p = &n;
	int a = reinterpret_cast<int>(p);

	cout << typeid(a).name() << a << endl;
	cout << typeid(p).name() << p << endl;
}

//const_cast用于解除const属性
void test3()
{
	volatile const int n = 10;
	int* p = const_cast<int*>(&n);
	(*p)++;
	cout << *p << endl;
	cout << n << endl;

	cout << typeid(n).name() << endl;
	cout << typeid(p).name() << endl;
}

//dynamic_cast用于向下转换--转换不成功返回0
// dynamic_cast只能用于父类含有虚函数的类
class Parent
{
public:
	Parent(int p = 0)
		:_p(p)
	{

	}

	virtual void func1()
	{

	}

	int _p;
};

class Child: public Parent
{
public:
	Child(int c=0)
		:_c(c)
	{}


	int _c;
};

void func(Parent* p)
{
	//Child* p1 = static_cast<Child*> (p);
	Child* p1 = dynamic_cast<Child*> (p);

	if (p1)
	{
		cout << "转换成功" << endl;
		p1->_c++;
		p1->_p++;
		cout << p1->_c << " " << p1->_p << endl;
	}
	else
	{
		cout << "转换失败" << endl;
	}

}

//RTTI--运行时即类型识别
//typeid
//dynamic_cast
//decltype

int main()
{
	test1();
	cout << "------------------------------------" << endl;

	test2();
	cout << "------------------------------------" << endl; 

	test3();

	cout << "------------------------------------" << endl;

	Parent p;
	Child c;
	func(&p);
	func(&c);

	return 0;
}